package com.bignerdranch.android.ecar.Adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.bignerdranch.android.ecar.Bean.MaintainBean;
import com.bignerdranch.android.ecar.R;
import com.bignerdranch.android.ecar.Utils.MyImageCache;
import com.bignerdranch.android.ecar.Utils.RequestUtil;
import com.bignerdranch.android.ecar.Utils.URLBank;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.List;


/**
 * Created by Administrator on 2016/7/25.
 */
public class UsedCarAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mInflater;
    private JSONArray mData;

    public UsedCarAdapter(Context context, JSONArray data) {
        mContext = context;
        mData = data;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mData.length();
    }

    @Override
    public Object getItem(int position) {
        try {
            return mData.get(position);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_used_car, null);
        }
        ViewHolder holder = new ViewHolder();
        if (convertView.getTag()==null){
            holder.imgRes = (NetworkImageView) convertView.findViewById(R.id.imgRes);
            holder.title = (TextView) convertView.findViewById(R.id.title);
            holder.mile = (TextView) convertView.findViewById(R.id.mile);
            holder.buy_date = (TextView) convertView.findViewById(R.id.buy_date);
            holder.price_now = (TextView) convertView.findViewById(R.id.price_now);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

//        holder.imgRes.setImageResource();
        try {
//            Log.e("yz","==="+mData.toString());
            String img_name = mData.getJSONObject(position).getString("image");
            String [] arr = img_name.split(",");
            String url_img = URLBank.teacher_url+URLBank.car_url+arr[0];
            ImageLoader loader = new ImageLoader(RequestUtil.getRequestQueue(mContext),new MyImageCache());
            holder.imgRes.setDefaultImageResId(R.drawable.sample);
            holder.imgRes.setErrorImageResId(R.drawable.sample_failed);
            holder.imgRes.setImageUrl(url_img,loader);

            holder.title.setText(mData.getJSONObject(position).getString("title").trim());
            holder.mile.setText(mData.getJSONObject(position).getString("km")+"Km");
            holder.buy_date.setText(mData.getJSONObject(position).getString("buyDate"));
            holder.price_now.setText(mData.getJSONObject(position).getString("price")+" 万");
//            Log.e("yz","=========="+mData.getJSONObject(position).getString("title").trim());
        } catch (JSONException e) {
//            Log.e("yz","==========error");
            e.printStackTrace();
        }

        return convertView;
    }

    public static class ViewHolder {
        public NetworkImageView imgRes;
        public TextView title;
        public TextView mile;
        public TextView buy_date;
        public TextView price_now;


    }
}
